package com.jrx.anytxn.param.service.partner.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.partner.PartnerShareProfitReq;
import com.jrx.anytxn.param.dto.partner.PartnerShareProfitRes;
import com.jrx.anytxn.param.entity.PrShareProfitTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrShareProfitTableMapper;
import com.jrx.anytxn.param.service.partner.IPartnerShareProfitService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 分润参数service实现
 */
@CacheConfig(cacheNames = "SHARE_PROFIT")
@Service
public class PartnerShareProfitServiceImpl implements IPartnerShareProfitService {


    private static final Logger logger = LoggerFactory.getLogger(PartnerShareProfitServiceImpl.class);

    @Autowired
    private ISegmentService segmentService;
    @Resource
    private ExtPrShareProfitTableMapper extPrShareProfitTableMapper;

    @Override
    public PartnerShareProfitRes getByTenIdIdAndShareProfitId(String tenantId, String shareProfitId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(shareProfitId)) {
            logger.error("商户ID或分润参数ID为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrShareProfitTable prShareProfitTable = extPrShareProfitTableMapper.selectByShareProfitIdAndTenantId(shareProfitId,tenantId);
        if (null == prShareProfitTable) {
            logger.error("未查到分润参数");
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        return BeanMapping.copy(prShareProfitTable, PartnerShareProfitRes.class);
    }

    @Override
    public PartnerShareProfitRes addPrShareProfitTable(PartnerShareProfitReq partnerShareProfitReq) throws TxnException {
        PrShareProfitTable table = extPrShareProfitTableMapper.selectByShareProfitIdAndTenantId(
                partnerShareProfitReq.getShareProfitId(),partnerShareProfitReq.getTenantId());
        if (table != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnBizException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrShareProfitTable prShareProfitTable = BeanMapping.copy(partnerShareProfitReq, PrShareProfitTable.class);
        try {
            prShareProfitTable.setId(segmentService.getId(Constant.PR_SHARE_PROFIR_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
        }
        prShareProfitTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrShareProfitTableMapper.insertSelective(prShareProfitTable);
        return BeanMapping.copy(partnerShareProfitReq, PartnerShareProfitRes.class);
    }

    @Override
    public TxnPage<PartnerShareProfitRes> findAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PartnerShareProfitRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PrShareProfitTable> tableList = extPrShareProfitTableMapper.selectByTenId(tenantId);
        if (CollectionUtils.isEmpty(tableList)) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PartnerShareProfitRes> res = BeanMapping.copyList(tableList, PartnerShareProfitRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @CacheEvict(key="'share_'+#partnerShareProfitReq.tenantId+'_'+#partnerShareProfitReq.shareProfitId")
    @Override
    public PartnerShareProfitRes modifyPartnerShareProfitTable(PartnerShareProfitReq partnerShareProfitReq) throws TxnBizException {
        PrShareProfitTable table = extPrShareProfitTableMapper.selectByShareProfitIdAndTenantId(partnerShareProfitReq.getShareProfitId(),
                partnerShareProfitReq.getTenantId());
        if (table == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        PrShareProfitTable prShareProfitTable = BeanMapping.copy(partnerShareProfitReq, PrShareProfitTable.class);
        // 更新
        extPrShareProfitTableMapper.updateByShareProfitIdAndTenantId(prShareProfitTable);
        return BeanMapping.copy(partnerShareProfitReq, PartnerShareProfitRes.class);
    }

    @CacheEvict(key="'share_'+#tenantId+'_'+#shareProfitId")
    @Override
    public Boolean removeByTenantIdAndShareProfitId(String tenantId, String shareProfitId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(shareProfitId)) {
            logger.error("租户id或分润参数id不可为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrShareProfitTableMapper.deleteByShareProfitIdAndTenantId(shareProfitId,tenantId);
        return i > 0;
    }

    @Cacheable(key="'share_'+#tenantId+'_'+#shareProfitId")
    @Override
    public PrShareProfitTable getByShareProfitId(String tenantId, String shareProfitId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(shareProfitId)) {
            logger.error("租户ID或分润参数id为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrShareProfitTable table = extPrShareProfitTableMapper.selectByShareProfitIdAndTenantId(
                shareProfitId,tenantId);
        return table;
    }

    @Override
    public List<PartnerShareProfitRes> getPartnerShareProfitByOrgIdAndTenId(String organizationId, String tenantId) {
        List<PrShareProfitTable> tableList = extPrShareProfitTableMapper.selectByTenIdAndOrgId(tenantId,organizationId);
        return BeanMapping.copyList(tableList, PartnerShareProfitRes.class);
    }
}
